layui.use(['laydate', 'laypage', 'form', 'layer'], function () {
        let laypage = layui.laypage;
        let form = layui.form;
        let layer = layui.layer;
        //监听提交
        form.on('submit(search)',
            function (data) {
                //分页完整功能
                laypage.render({
                    elem: 'demo7',
                    count: getGlobalNum(data),
                    layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                    jump: function (obj) {
                        let userVo = {
                            "startDate": data.field.startDate,
                            "endDate": data.field.endDate,
                            "userModel": {"username": data.field.username}
                        };
                        let pageData = {"count": obj.count, "curr": obj.curr, "pages": obj.pages, "limit": obj.limit};

                        let userPageVo = {};
                        userPageVo.userModelVo = userVo;
                        userPageVo.pageModel = pageData;

                        console.log("模糊查询参数", userPageVo)
                        $.ajax({
                            url: "http://localhost:8080/back/user/globalSearch",
                            dataType: "json",
                            contentType: 'application/json',
                            data: JSON.stringify(userPageVo),
                            type: "post",
                            headers:{'username':localStorage.getItem("loginUser")},
                            success: function (res) {
                                var html;
                                console.log("模糊查询后端返回值", res)
                                //模糊查询没有返回值，刷新当前页面
                                if (res.data.length == 0) {
                                    layer.msg("不存在该类数据", function () {
                                        location.reload();
                                    })
                                }
                                for (var i = 0; i < res.data.length; i++) {
                                    html += ' <tr>\n' +
                                        '                                    <td>\n' +
                                        '                                      <input type="checkbox" name="id" value="1"   lay-skin="primary"> \n' +
                                        '                                    </td>\n' +
                                        '                                    <td>' + res.data[i].id + '</td>\n' +
                                        '                                    <td>' + res.data[i].username + '</td>\n' +
                                        '                                    <td>' + res.data[i].sex + '</td>\n' +
                                        '                                    <td>' + res.data[i].phone + '</td>\n' +
                                        '                                    <td>' + res.data[i].address + '</td>\n' +
                                        '                                    <td class="td-status">\n' +
                                        '                                      <span class="layui-btn layui-btn-normal layui-btn-mini ' + (res.data[i].enable == 1 ? "" : "layui-btn-disabled") + ' ">' + (res.data[i].enable == 1 ? "启用" : "停用") + '</span></td>\n' +
                                        '                                    <td class="td-manage">\n' +
                                        '                                      <a onclick="memberGlobal_stop(this,' + res.data[i].id + ')" href="javascript:;"  title=' + (res.data[i].enable == 1 ?  "停用" : "启用")+ '\>\n' +
                                        '                                        <i class="layui-icon">' + (res.data[i].enable == 1 ? "&#xe601;" : "&#xe62f;") + '</i>\n' +
                                        '                                      </a>\n' +
                                        '                                      <a title="编辑"  onclick="sendGlobalId(' + res.data[i].id + ')"href="javascript:;">\n' +
                                        '                                        <i class="layui-icon">&#xe642;</i>\n' +
                                        '                                      </a>\n' +
                                        '                                      <a onclick="alertGlobalPass(' + res.data[i].id + ')" title="修改密码" href="javascript:;">\n' +
                                        '                                        <i class="layui-icon">&#xe631;</i>\n' +
                                        '                                      </a>\n' +
                                        '                                      <a title="删除" onclick="memberGlobaldel(this,' + res.data[i].id + ')" href="javascript:;">\n' +
                                        '                                        <i class="layui-icon">&#xe640;</i>\n' +
                                        '                                      </a>\n' +
                                        '                                    </td>\n' +
                                        '                                  </tr>'

                                }
                                $("#userInfo").html(html);

                            }
                        })
                    }
                })
                return false
            })
    }
)


//得到记录条数
function getGlobalNum(data) {


    let userVo = {
        "startDate": data.field.startDate,
        "endDate": data.field.endDate,
        "userModel": {"username": data.field.username}
    };
    // let userVo = {"startDate":data.field.startDate,"endDate":data.field.endDate};
    // let userModel = {"username":data.field.username};
    // userVo.userModel = userModel;

    let count = 0;
    $.ajax({
        url: "http://localhost:8080/back/user/getGlobalSearchCount",
        dataType: "json",
        data: JSON.stringify(userVo),
        contentType: 'application/json',
        type: "post",
        headers:{'username':localStorage.getItem("loginUser")},
        async: false,//关闭异步
        success: function (res) {
            // console.log("分页返回值：",res)
            count = res.count;

        }
    })
    return count;
}

//修改密码
function alertGlobalPass(id) {
    xadmin.open('修改密码', '/html/user/member-password.html', 600, 400)
    sessionStorage.setItem("id", id);
}


//编辑普通信息
function sendGlobalId(id) {
    layui.use(['layer'], function () {
        let layer = layui.layer;
        xadmin.open('编辑', '/html/user/member-edit.html', 600, 400);
        console.log("id值", id);

        sessionStorage.setItem("id", id);

    })
}

//删除某一行用户
function memberGlobaldel(obj, id) {
    layui.use(['layer'], function () {
        layer.confirm('确认要删除吗？', function () {
            $.ajax({
                url: "/user/delete",
                type: "post",
                dataType: "json",
                data: {"id": id},
                success: function (res) {
                    if (res.msg == "success") {
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!', {icon: 1, time: 1000});
                    } else {
                        layer.msg('删除失败!', {icon: 1, time: 1000});
                    }


                }
            })
        });
    })
}

/*用户-停用*/
function memberGlobal_stop(obj, id) {
    layer.confirm('确认要改变可用状态吗？', function (index) {
        sessionStorage.setItem("id", id);
        value = {"id": id, "enable": 1}
        /*如果title为启用，表示该数据是停用的*/
        if ($(obj).attr('title') == '启用') {
            $.ajax({
                url: "http://localhost:8080/back/user/update",
                type: "post",
                data: JSON.stringify(value),
                contentType: "application/json",
                dataType: "json",
                success: function () {


                    $(obj).attr('title', '停用')
                    $(obj).find('i').html('&#xe601;');

                    $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('启用');
                    layer.msg('已启用!', {icon: 6, time: 1000});
                }

            })

        } else {
            value.enable = 0;
            $.ajax({
                url: "http://localhost:8080/back/user/update",
                type: "post",
                data: JSON.stringify(value),
                contentType: "application/json",
                dataType: "json",
                success: function () {
                    //发异步把用户状态进行更改
                    $(obj).attr('title', '启用')
                    $(obj).find('i').html('&#xe62f;');

                    $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('停用');
                    layer.msg('已停用!', {icon: 5, time: 1000});
                }

            })

        }

    });
}